package admin.dao.impl;

import admin.dao.AdminDao;
import util.DBUtil;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;


public class AdminDaoImpl extends DBUtil implements AdminDao {

    private static final String SQL_LOGIN = " select id from eadmin where username=? and password=? ";
    private static final String SQL_UPDATE_STATUS = " update eadmin set logintime=?,loginip=? where username=? ";

    @Override
    public boolean login(String userName, String passWord) {
        ArrayList params = new ArrayList();
        params.add(userName);
        params.add(passWord);

        try {

            resultSet = query(SQL_LOGIN,params);
            //resultSet.next()为是否有下一个元素，没有则为false，有则为true
            return resultSet.next();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return false;
    }

    @Override
    public void updateLoginTime(String userName, Date date, String ip) {
        ArrayList params = new ArrayList();
        //需要转下Date数据类型,在MySQL里可以使用的Date对象
        params.add(new Timestamp(date.getTime()));
        params.add(ip);
        params.add(userName);

        try {
            //更新数据库用户登录时间和登录ip
            update(SQL_UPDATE_STATUS,params);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
    }
}
